<?php
namespace util;


class Tree
{
    public static function buildTree($data, $list, $pid) {
        foreach ($list as $key => $v) {
            if ($v['parent_id'] == $pid) {
                $v['children'] = [];
                $children = self::buildTree($v['children'], $list, $v['id']);
                if($children) {
                    $v['children'] = $children;
                } else {
                    unset($v['children']);
                }
                array_push($data, $v);
            }
        }

        return $data;
    }

    public static function buildTree2($data, $list, $pid) {
        foreach ($list as $key => $v) {
            if ($v['pid'] == $pid) {
                $v['children'] = [];
                $children = self::buildTree2($v['children'], $list, $v['id']);
                if($children) {
                    $v['children'] = $children;
                } else {
                    unset($v['children']);
                }
                array_push($data, $v);
            }
        }

        return $data;
    }
}